﻿using A2.ERP.BLL;
using A2.ERP.Models;
using A2.ERP.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using A2.ERP.IBLL;

namespace A2.ERP.UI.Controllers
{
    public class StoreController : Controller
    {
        t_AppendStockDetailService _appedService = DBService.GetService<t_AppendStockDetailService>();

        t_ProductsService _productsService = DBService.GetService<t_ProductsService>();
        t_SupplierService _supperlierService = DBService.GetService<t_SupplierService>();
        t_ProductsTypeService _ProductsType = DBService.GetService<t_ProductsTypeService>();
        t_UserService _userService = DBService.GetService<t_UserService>();
        t_StoreHouseDetailService _SubareaName = DBService.GetService<t_StoreHouseDetailService>();

        It_ProductsService _Products = DBService.GetService<t_ProductsService>();


        t_StoreHouseService _HouseName = DBService.GetService<t_StoreHouseService>();
        //库存盘点
        t_InventoryService _InventoryService = DBService.GetService<t_InventoryService>();
        //库存调拨
        t_TransferringOrderService _TransferringOrderService = DBService.GetService<t_TransferringOrderService>();
        //入库
        It_AppendStockService _AppendStockService = DBService.GetService<t_AppendStockService>();
        It_AppendStockDetailService _AppendStockDetailService = DBService.GetService<t_AppendStockDetailService>();

        t_ProductsService _Productname = DBService.GetService<t_ProductsService>();
        t_ServiceTypeService _SerViceName = DBService.GetService<t_ServiceTypeService>();
        It_SupplierService _supplierService = DBService.GetService<t_SupplierService>();

        // GET: /Store/
        //仓库管理模块

        //商品
        [HttpPost]
        public ActionResult ProductsList()
        {
            var obj = _Products.LoadEntities(i => true).ToList();

            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        //供应商
        [HttpPost]
        public ActionResult supplierList()
        {
            var obj = _supplierService.LoadEntities(i => true).ToList();

            return Json(obj, JsonRequestBehavior.AllowGet);
        }
       
        #region 物品出库模块
        public ActionResult OutStockModule()
        {
            //下拉列表（出入库类型）
            ViewBag.typeList5 = _SerViceName.LoadEntities(i => true);
            //下拉列表（商品类型）
            ViewBag.typeList = _ProductsType.LoadEntities(i => true);
            //下拉列表（库区）
            ViewBag.typeList2 = _SubareaName.LoadEntities(i => true);
            //下拉列表（库区）
            ViewBag.typeList3 = _HouseName.LoadEntities(i => true);
            //下拉列表（盘点商品）
            ViewBag.typeList4 = _Productname.LoadEntities(i => true);
            //LINQ(三表查询)--操作部位
            //ViewBag.data = (from a in _appedService.LoadEntities(i => true)
            //                join p in _productsService.LoadEntities(i => true)
            //                    on a.ProductsID equals p.ProductsID
            //                join s in _supperlierService.LoadEntities(i => true)
            //                    on a.SupplierID equals s.SupplierID
            //                select new v_AppendModule
            //                {
            //                    AppendID = a.AppendID,
            //                    ProductsName = p.ProductsName,
            //                    TotalSalesNum = p.TotalSalesNum,
            //                    Price = a.Price,
            //                    Cost = p.Cost
            //                }).ToList();
            return PartialView();
        }
        //删除--物品出库
        [HttpDelete]
        public ActionResult OutStockModule(string id)
        {
            _appedService.Procedures<int>("exec p_AppendStock_deleteEitity @id", new SqlParameter[]
            {
                new SqlParameter { ParameterName = "@id", Value = id}
            });

            return Json(true);
        }
        //增加--物品出库
        [HttpPost]
        public ActionResult OutStockModule(p_AppendStock_getByID_Result entity)
        {
            var token = Request.Cookies["uid"].Value;
            var userName = _userService.LoadEntities(i => i.Token == token).FirstOrDefault().UserName;
            _appedService.Procedures<int>("exec p_AppendStock_insertNewEntity @id, @type, @houseId, @detailId, @userName, @tradeDate, @totailPrice, @alreadyPay, @description, @state, @createDate", new SqlParameter[]
            {
                new SqlParameter { ParameterName = "@id", Value = entity.AppendID},
                new SqlParameter { ParameterName = "@type", Value = entity.AppendType},
                new SqlParameter { ParameterName = "@houseId", Value = entity.StoreHouseID},
                new SqlParameter { ParameterName = "@detailId", Value = entity.HouseDetailID},
                new SqlParameter { ParameterName = "@userName", Value = userName},
                new SqlParameter { ParameterName = "@tradeDate", Value = entity.TradeDate},
                new SqlParameter { ParameterName = "@totailPrice", Value = 0},
                new SqlParameter { ParameterName = "@alreadyPay", Value = entity.AlreadyPay},
                new SqlParameter { ParameterName = "@description", Value = entity.Description},
                new SqlParameter { ParameterName = "@state", Value = entity.State},
                new SqlParameter { ParameterName = "@createDate", Value = entity.CreateDate},
            });

            return Json(true);
        }

        #endregion

        //2.物品入库模块
        public ActionResult AppendStockModule()
        {
            //下拉列表（出入库类型）
            ViewBag.typeList5 = _SerViceName.LoadEntities(i => true);
            //下拉列表（库区）
            ViewBag.typeList2 = _SubareaName.LoadEntities(i => true);
            //下拉列表（库区）
            ViewBag.typeList3 = _HouseName.LoadEntities(i => true);
            //LINQ(三表查询)--操作部位

            return PartialView();
        }

        //添加
        //[HttpPost]
        //public ActionResult AddStockModule()
        //{
        //    var result = _AppendStockService.Procedures<int>("exec p_AppendStockDetail_insertNewEntity @AppendID,@ProductsID,@SupplierID,@Quantity,@Price,@Description", new SqlParameter[]
        //    {
        //        new SqlParameter { ParameterName = "@ReceiptOrderID", ""},
        //        new SqlParameter { ParameterName = "@ReceiptOrderID", ""},
        //        new SqlParameter { ParameterName = "@ReceiptOrderID", ""},
        //        new SqlParameter { ParameterName = "@ReceiptOrderID", ""},
        //        new SqlParameter { ParameterName = "@ReceiptOrderID", ""},
        //        new SqlParameter { ParameterName = "@ReceiptOrderID", ""}
        //    });

        //    return Json(result);
        //}


        //删除--物品入库
        //[HttpDelete]
        //public ActionResult AppendStockModule(string id)
        //{
        //   var resutl=_appedService.Procedures<int>("exec p_AppendStock_deleteEitity @id", new SqlParameter[]
        //    {
        //        new SqlParameter { ParameterName = "@id", Value = id}
        //    });

        //    //var result1 = _AppendStockService.DeleEntity(new t_AppendStock() {AppendID=id });
        //    //var result2 = _AppendStockDetailService.DeleEntity(new t_AppendStockDetail() { AppendID = id });

        //    return Json("true");
        //}

        //查询--物品入库 
        [HttpPost]
        public ActionResult updatesanbiao(string id)
        {
            var result  = (from a in _appedService.LoadEntities(i => true)
                                         join p in _productsService.LoadEntities(i => true)
                                             on a.ProductsID equals p.ProductsID
                                         join s in _supperlierService.LoadEntities(i => true)
                                             on a.SupplierID equals s.SupplierID
                                             where a.AppendID==id
                                         select new v_AppendModule
                                         {
                                             AppendID = a.AppendID,
                                             ProductsName = p.ProductsName,
                                             SupplierName = s.SupplierName,
                                             TotalSalesNum = p.TotalSalesNum,
                                             Price = p.Price,
                                             Cost = p.Cost
                                         }).ToList();

            return Json(result);
        }

        [HttpPut]
        public ActionResult AppendStockModule(t_AppendStockDetail entity)
        {
            var result = _appedService.UpdateEntity(entity);

            return Json(result);
        }
        //增加--物品入库

        [HttpPost]
        public ActionResult AddAppendStockModule(t_AppendStock entity)
        {
            //SqlParameter[] ps = new SqlParameter[]{
            //    new SqlParameter("@HouseID",HouseID),
            //     new SqlParameter("@HouseID",HouseID),
            //      new SqlParameter("@HouseID",HouseID),
            //       new SqlParameter("@HouseID",HouseID),
            //        new SqlParameter("@HouseID",HouseID),
            //};

            var result = _AppendStockService.AddEntity(entity);
            //@AppendID varchar(20),
            //@ProductsID int,
            //@SupplierID int,
            //@Quantity int,
            //@Price money,
            //@Description varchar(200)

            return Json(entity);
        }

        //3.库存调拨模块
        public ActionResult TransferringModule()
        {

            //下拉列表（商品类型）
            ViewBag.typeList = _ProductsType.LoadEntities(i => true);
            //下拉列表（库区）
            ViewBag.typeList2 = _SubareaName.LoadEntities(i => true);

            return PartialView();
        }
        [HttpPost]
        public ActionResult AddTransferring(t_TransferringOrder entity)
        {
            entity.AuditingUser = Request.Cookies["uid"].Value;
            entity.UserName = Request.Cookies["uid"].Value;
            var result = _TransferringOrderService.AddEntity(entity);
            return Json(entity);

        }
        //4.仓库盘点
        public ActionResult InventoryModule()
        {
            ViewBag.typeList2 = _SubareaName.LoadEntities(i => true);
            //下拉列表（库区）
            ViewBag.typeList3 = _HouseName.LoadEntities(i => true);
            //下拉列表（盘点商品）
            ViewBag.typeList4 = _Productname.LoadEntities(i => true);

            return PartialView();
        }
        [HttpPost]
        public ActionResult AddInventory(t_Inventory entity)
        {
            entity.AuditingUser = Request.Cookies["uid"].Value;
            entity.TradeDate = DateTime.Now.ToString();
            entity.UserName = Request.Cookies["uid"].Value;
            var result = _InventoryService.AddEntity(entity);
            return Json(result);
        }
    }
}
